/**
 * 基于Jquery气泡消息控件
 * @author wonderful-WuDaoHong wuketidai@foxmail.com
 * @create 20143027
 * @version 1.0.0
 * @description 
 */
 
(function(){
 
	var option={
			bubName:"",//要触发气泡信息框的气泡名
			title:"",//气泡的标题
			remoteData:true,//是否获取后台数据
			url:"${ctx}action/portal/index/companyDetail.action",
			keys:[//键定义
			      {
			    	key:"",//要展示数据的键名
			    	label:"",//键显示的名称
			    	keyStyle:"",//自定义的样式属性
			        valueStyle:""//自定义的样式属性	
			      }
			      ]
	};
	
	
 $.fn.extend({"wdfBubble":function(operat,option,data){
	 if(operat=='init'){
		var strIDS=''
		 this.find("[bubName='"+option.bubName+"']").each(function(index){
			 /*var bub=createBub(option);
			 $("body").append(bub);
			 bub.css({left:$(this).offset().left+$(this).width()/2,top:$(this).offset().top+$(this).height()+8});
			 fillData(option,data,bub,$(this));*/
			
			    strIDS+=$(this).attr("bubParam")+",";
			    
		 });
		if(strIDS&&strIDS.length!=0)
			{
			strIDS=strIDS.substring(0,strIDS.length-1);
			}
		 fillAllData(option,null,this,strIDS);
        }
	 }});

 function createBub(option){
	var bub=$("<div class='wdfBubble_common'></div>");
	var arrows=$("<div class='arrows'></div>");
	var title=$("<div class='title'></div>");
	var content=$("<div class='content'></div>");
	title.append(option.title);
	bub.append(arrows);
	content.append(title);
	bub.append(content);
	return bub;
 }
 
 function fillData(option,data,bub,evenObj)
 {
	 if(data)
		 {
		 bub.find(".content").append(createKV(option,data));
		 evenObj.data("bub",bub);
		 evenObj.css("cursor","pointer");
		 evenObj.bind("mouseover",function(){
			 var bub=$(this).data("bub");
			 bub.css("display","block");
			 bub.animate({opacity:1},1);
		 });
		 evenObj.bind("mouseout",function(){
			 var bob=$(this).data("bub");
			 bub.animate({opacity:0},1);
			 setTimeout(function(){
				 bub.css("display","none");
			 },1);
		 });
		 }
	 else{
		 $.ajax({
				url :option.url,
				//processData: false,
				//data: xmlDocument,
				async :true,
				dataType : "json",
				type : "POST",
				contentType : "application/x-www-form-urlencoded",
				data :$(evenObj).attr("bubParam"),
				beforeSend : null,
				success : function(data){
					 bub.find(".content").append(createKV(option,data));
					 evenObj.data("bub",bub);
					 evenObj.css("cursor","pointer");
					 evenObj.bind("mouseover",function(){
						 var bub=$(this).data("bub");
						 bub.css("display","block");
						 bub.animate({opacity:1},1);
					 });
					 evenObj.bind("mouseout",function(){
						 var bob=$(this).data("bub");
						 bub.animate({opacity:0},1);
						 setTimeout(function(){
							 bub.css("display","none");
						 },1);
					 });
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
				  
					alert(textStatus);

				}
				
			});	
	 }
 }
 
 function fillAllData(option,data,contentObj,ids)
 {
	 if(data)
		 {
		 bub.find(".content").append(createKV(option,data));
		 evenObj.data("bub",bub);
		 evenObj.css("cursor","pointer");
		 evenObj.bind("mouseover",function(){
			 var bub=$(this).data("bub");
			 bub.css("display","block");
			 bub.animate({opacity:1},1);
		 });
		 evenObj.bind("mouseout",function(){
			 var bob=$(this).data("bub");
			 bub.animate({opacity:0},1);
			 setTimeout(function(){
				 bub.css("display","none");
			 },1);
		 });
		 }
	 else{
		 $.ajax({
				url :option.url,
				//processData: false,
				//data: xmlDocument,
				async :true,
				dataType : "json",
				type : "POST",
				contentType : "application/x-www-form-urlencoded",
				data :"ids="+ids,
				beforeSend : null,
				success : function(data){
					 
					contentObj.find("[bubName='"+option.bubName+"']").each(function(index){
						    var bubInfo=data[$(this).attr("bubParam")];
						    var evenObj=$(this);
						    if(bubInfo)
						    	{
						    	var bub=createBub(option);
								$("body").append(bub);
								bub.css({left:evenObj.offset().left+evenObj.width()/2,top:evenObj.offset().top+evenObj.height()+8});
								bub.find(".content").append(createKV(option,bubInfo));
								 evenObj.data("bub",bub);
								 evenObj.css("cursor","pointer");
								 evenObj.bind("mouseover",function(){
									 var bub=$(this).data("bub");
									 bub.css("display","block");
									 bub.animate({opacity:1},1);
								 });
								 evenObj.bind("mouseout",function(){
									 var bob=$(this).data("bub");
									 bub.animate({opacity:0},1);
									 setTimeout(function(){
										 bub.css("display","none");
									 },1);
								 });
						    	}
					 });
					
					
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
				  
					alert(textStatus);

				}
				
			});	
	 }
 }
 
 
 
 function createKV(option,data){
	 
	 var ul=$("<ul></ul>");
	 
	 $(option.keys).each(function(index){
		 var li=$("<li></li>");
		 var key=$("<label class='key'></label>");
		 var value=$("<label class='value'></label>");
		 value.append(data[this.key]);
		 key.append(this.label);
		 li.append(key);
		 li.append(value);
		 if(this.keyStyle)
			 {
			 key.attr("style",this.keyStyle);
			 }
		 if(this.valueStyle)
		    {
		    value.attr("style",this.valueStyle);
		    }
		 ul.append(li);
	 });
	 return ul;
 }
 
 })()